package com.bawei.hgshop.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.bawei.hgshop.entity.Sku;
import com.bawei.hgshop.entity.SkuVo;
import com.bawei.hgshop.entity.SpecOption;
import com.bawei.hgshop.entity.SpuVo;

public interface SkuMapper {


	@Insert("insert into hg_sku(title,sell_point,price,stock_count,barcode,image,status,create_time,update_time,cost_price,market_price,spu_id,cart_thumbnail)"
			+ "VALUES(#{title},#{sellPoint},#{price},#{stockCount},#{barcode},#{image},1,now(),now(),#{costPrice},#{marketPrice},#{spuId},#{cartThumbnail})"
			  )
	@Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
	int addSku(Sku sku);	

	@Insert("insert into hg_sku_spec(sku_id,spec_id,spec_option_id) "
			+ "values(#{id},#{op.specId},#{op.id})")
	void addSkuOptions(@Param("id") int id,@Param("op") SpecOption option);

	/**
	 * 
	 * @param skuVo
	 * @return
	 */
	List<Sku> list(SkuVo skuVo);

	Sku findById(int id);

	List<Sku> listBySpec(SpuVo spuVo);

	// 根据一个规格属性id 获取一组skuId 
	/**
	 * 
	 * @param spuId
	 * @param id2
	 * @param specId
	 * @return
	 */
	@Select(" SELECT sk.id from hg_sku sk , hg_sku_spec sp "
			+ " WHERE  sk.id=sp.sku_id "
			+ " and sk.spu_id =#{spuId} "
			+ " and sp.spec_id  =#{specId}  "
			+ " and sp.spec_option_id  =#{optionId}  ")
	List<Integer> listBySpuOption(@Param("spuId") int spuId, @Param("optionId") int optionId, @Param("specId") int specId);

	// 获取一个spu 下所有的sku 的id
	@Select(" SELECT id from hg_sku  where spu_id =#{spuId}  ")
	List<Integer> listIdbySpuId(int id);

}
